package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
import java.util.NoSuchElementException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 10715
 * Date: 2024-08-27
 * Time: 20:22
 */
public class DetailPage extends Utils {
    Class<?> targetclass = DetailPage.class;

    public static String url = "http://192.168.47.135:8653/blog_detail.html?id=19";

    public DetailPage() {
        super(url);
    }

    /*用户是否可以看到是自己的文章*/
    public void detailByMe() {
        //已经登陆
        driver.navigate().to(url);
        driver.navigate().refresh();
        String methodName = "";
        String name = "zhangsan";
        WebElement element1 = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3"));
        assert element1.getText().equals(name);
        //查看是否有编辑按钮
        WebElement element2 = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)"));
        assert element2.getText().equals("编辑");
        //截屏
        try {
            methodName = targetclass.getMethod("detailByMe").getName();
            getScreenShot(targetclass.getName() + "-" + methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public void detailByMeToDelete() {
        //已经登陆
        driver.navigate().refresh();
        driver.navigate().to(url);
        String methodName = "";
        String name = "zhangsan";
        WebElement element1 = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3"));
        assert element1.getText().equals(name);
        //查看是否有删除按钮
        WebElement element2 = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)"));
        assert element2.getText().equals("删除");
        element2.click();
        WebDriverWait webDriverWait=new WebDriverWait(driver, Duration.ofSeconds(3));
        webDriverWait.until(ExpectedConditions.alertIsPresent());
        Alert alert =driver.switchTo().alert();
        String  text =alert.getText();
        alert.accept();
        //再访问
        driver.navigate().to(url);
        //是否为list页面
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title"));
        //截屏
        try {
            methodName = targetclass.getMethod("detailByMeToDelete").getName();
            getScreenShot(targetclass.getName() + "-" + methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("detailByMeToDelete---"+text);
    }

    /*
    其他人的博客详情
     */
    public void detailByOther() {
        driver.navigate().refresh();
        driver.get("http://192.168.47.135:8653/blog_detail.html?id=10");
        //查看是否与是本人？
        WebElement element1 = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3"));
        assert element1.getText().equals("lisi");
        //是否可以编辑
        try {
            driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)"));
        } catch (NoSuchElementException e) {
            System.out.println("detailByOther ----不是本人页面没有编辑按钮");
        }
        //截屏
        String methodName = "";
        try {
            methodName = targetclass.getMethod("detailByOther").getName();
            getScreenShot(targetclass.getName() + "-" + methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
